<?php
namespace Site\Controller;
class ArticleController extends CommonController {
    /**
     * 未审核文章
     *
     * @param    void
     * @return    void
     **/
    public function unpass() {
        $size = 12;
        $cid = I('get.cid',0,'intval');

        $categoryList = D('Category','Mysql')->getList(array(
            'sid'       =>  $this->sid,
        ),'id,title');
        if(!$categoryList) {
            $this->error('网站' . $this->sitename . '没有可用分类');
        }
        if(!$cid) {
            $cid = intval($categoryList[0]['id']);
        }

        $where = $cid ? array('cid.'.$cid=>$cid,'status'=>1) : array('status'=>1);

        $order = 'createtime desc';

        $ArticleMongoModel = D('Article','Mongo');

        $count = $ArticleMongoModel->where($where)->count();

        $Page = new \Think\Page($count,$size);

        $page = $Page->show();

        $list = $ArticleMongoModel->limit($Page->firstRow . ',' . $Page->listRows)->where($where)->order($order)->select();

        $data = array(
            'list'  =>  $list,
            'pager'  =>  $page,
            'count' =>  $count,
            'size'  =>  $size,
            'cid'   =>  $cid,
            'categoryList'  =>  $categoryList
        );

        $this->assign($data);

        $this->display();
    }
    /**
     * 审核文章
     *
     * @param    void
     * @return    void
     **/
    public function review() {
        if(IS_POST) {
            $ArticleModel = D('Article','Mysql');
            $ArticleModel->review(array_merge(I('post.'),array(
                'content'   =>  $_POST['content']
            )));
            $this->success('文章审核成功','/article/unpass?cid='.I('post.cid'));
        }else {
            $id = I('get.id',0,'intval');
            if(!$id) {
                $this->error('文章ID不存在');
            }
            $cid = I('get.cid',0,'intval');
            if(!$cid) {
                $this->error('分类ID不存在');
            }
            $categoryInfo = M('Category')->find($cid);
            if(!$categoryInfo) {
                $this->error('分类不存在');
            }

            $categoryList = M('Category')->where("sid={$this->sid}")->select();
            if(!$categoryList) {
                $this->error('当前网站还没有添加分类');
            }

            $ArticleMongoModel = D('Article','Mongo');
            $articleInfo = $ArticleMongoModel->where(array('id'=>$id,'cid.'.$cid=>$cid))->find();
            if(!$articleInfo) {
                $this->error('文章不存在');
            }

            $this->assign(array(
                'articleInfo'   =>  $articleInfo,
                'categoryInfo'  =>  $categoryInfo,
                'categoryList'  =>  $categoryList,
                'cid'           =>  $cid,
            ));
            $this->display();
        }
    }
    /**
     * 已审核文章
     *
     * @param   void
     * @return  void
     **/
    public function pass() {
        $size = 12;
        $cid = I('get.cid',0,'intval');

        $categoryList = D('Category','Mysql')->getList(array(
            'sid'       =>  $this->sid,
        ),'id,title');

        if(!$categoryList) {
            $this->error('网站' . $this->sitename . '没有可用分类');
        }

        $where = $cid ? array('cid'=>$cid) : array('sid'=>$this->sid);

        $order = 'publishtime desc';

        $data = $this->getPageList(M('Article'),$where,$order);

        $this->assign(array_merge($data,array(
            'count' =>  $count,
            'size'  =>  $size,
            'cid'   =>  $cid,
            'categoryList'  =>  $categoryList
        )));

        $this->display();
    }
    /**
     * 添加文章
     *
     * @param    void
     * @return    void
     **/
    public function add() {
        $this->display();
    }
    /**
     * 修改文章
     *
     * @param    void
     * @return    void
     **/
    public function update() {
        if(IS_POST) {
            $ArticleModel = D('Article','Mysql');
            $ArticleModel->update(I('post.id'),array_merge(I('post.'),array(
                'content'   =>  $_POST['content']
            )));
            $this->success('文章《'.I('post.title').'》修改成功','/article/pass?cid='.I('post.cid'));

        }else {
            $id = I('get.id',0,'intval');
            if(!$id) {
                $this->error('文章ID不存在');
            }

            $categoryList = D('Category','Mysql')->getList(array(
                'sid'       =>  $this->sid,
            ),'id,title');
            if(!$categoryList) {
                $this->error('网站' . $this->sitename . '没有可用分类');
            }

            $ArticleModel = D('Article','Mysql');
            $articleInfo = $ArticleModel->find($id);
            if(!$articleInfo) {
                $this->error('文章不存在');
            }

            if($articleInfo['sid'] != $this->sid) {
                $this->error('无权修改文章《'.$articleInfo['title'].'》');
            }

            $this->assign(array(
                'articleInfo'   =>  $articleInfo,
                'categoryList'  =>  $categoryList,
                'cid'           =>  I('get.cid'),
            ));
            $this->display();
        }
    }

    public function remove() {
        $id = I('get.id',0,'intval');
        if(!$id) {
            $this->error('文章ID不存在');
        }
        $cid = I('get.cid',0,'intval');
        if(!$cid) {
            $this->error('分类ID不存在');
        }
        $categoryInfo = M('Category')->find($cid);
        if(!$categoryInfo) {
            $this->error('分类不存在');
        }

        $ArticleMongoModel = D('Article','Mongo');
        $articleInfo = $ArticleMongoModel->where(array('id'=>$id,'cid.'.$cid=>$cid))->find();
        if(!$articleInfo) {
            $this->error('文章不存在');
        }
        $result = $ArticleMongoModel->save(array(
            'id'            =>  $id,
            'cid.'.$cid     =>  0,
            'updatetime'    =>  NOW_TIME
        ));
        if($result === false) {
            $this->error('文章删除失败');
        }else {
            $this->success('文章 "'.$articleInfo['title'].'" 删除成功','/article/unpass?cid='.$cid);
        }
    }

    public function offline() {
        $id = I('get.id',0,'intval');
        if(!$id) {
            $this->error('文章ID不存在');
        }
        $ArticleModel = D('Article','Mysql');
        $articleInfo = $ArticleModel->where(array('id'=>$id))->field('id,title,sid,status')->find();
        if(!$articleInfo) {
            $this->error('文章不存在');
        }
        if($articleInfo['sid'] != $this->sid) {
            $this->error('无权修改,文章《'.$articleInfo['title'].'》');   
        }
        if(!$articleInfo['status']) {
            $this->error('文章《'.$articleInfo['title'].'》已经下线');
        }

        $ArticleModel->where("id={$id}")->save(array(
            'status'    =>  0
        ));

        $this->success('文章《'.$articleInfo['title'].'》下线成功');
    }

    public function online() {
        $id = I('get.id',0,'intval');
        if(!$id) {
            $this->error('文章ID不存在');
        }
        $ArticleModel = D('Article','Mysql');
        $articleInfo = $ArticleModel->where(array('id'=>$id))->field('id,title,sid,status')->find();
        if(!$articleInfo) {
            $this->error('文章不存在');
        }
        if($articleInfo['sid'] != $this->sid) {
            $this->error('无权修改,文章《'.$articleInfo['title'].'》');   
        }
        if($articleInfo['status']) {
            $this->error('文章《'.$articleInfo['title'].'》已经上线');
        }

        $ArticleModel->where("id={$id}")->save(array(
            'status'    =>  1
        ));

        $this->success('文章《'.$articleInfo['title'].'》上线成功');
    }

    public function getAjax() {
        $id = I('get.id',0,'intval');
        if(!$id) {
            $this->errorAjax('文章ID不存在');
        }
        $ArticleModel = D('Article','Mysql');
        $where = array(
            'id'    =>  $id,
            'sid'   =>  $this->sid,
            'status'=>  1,
            'publishtime'   =>  array('lt',NOW_TIME),
        );

        $articleInfo = $ArticleModel->where($where)->field('id,sid,title,status')->find();
        if(!$articleInfo) {
            $this->errorAjax('文章不存在');
        }
        if($articleInfo['sid'] != $this->sid) {
            $this->errorAjax('无权修改,文章《'.$articleInfo['title'].'》');   
        }
        if(!$articleInfo['status']) {
            $this->errorAjax('文章《'.$articleInfo['title'].'》已经下线');
        }

        $articleInfo['title'] = subTitle($articleInfo['title']);

        $this->successAjax($articleInfo);
    }
}
?>
